package com.xcc.httpUtils.request;

import com.xcc.httpUtils.HttpUtils;
import com.xcc.httpUtils.callback.BaseHttpCallBack;
import okhttp3.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * Created by Carman on 2017/7/7.
 * <p>
 * 请求调用类
 */
public class RequestCall {

    private final long DEFAULT_TIME = 3000;

    private BaseHttpRequest okHttpRequest;
    private Request request;
    private Call call;
    private OkHttpClient client;

    private long readTimeOut;
    private long writeTimeOut;
    private long connTimeOut;

    /**
     * @param request 请求类
     */
    public RequestCall(BaseHttpRequest request) {
        this.okHttpRequest = request;
    }

    /**
     * 设置读取超时
     *
     * @param readTimeOut 时间,单位毫秒
     * @return RequestCall 当前对象
     */
    public RequestCall readTimeOut(long readTimeOut) {
        this.readTimeOut = readTimeOut;
        return this;
    }

    /**
     * 设置写入超时
     *
     * @param writeTimeOut 时间, 单位毫秒
     * @return RequestCall 当前对象
     */
    public RequestCall writeTimeOut(long writeTimeOut) {
        this.writeTimeOut = writeTimeOut;
        return this;
    }

    /**
     * 设置连接超时
     *
     * @param connTimeOut 时间, 单位毫秒
     * @return RequestCall 当前对象
     */
    public RequestCall connTimeOut(long connTimeOut) {
        this.connTimeOut = connTimeOut;
        return this;
    }

    /**
     * 获取 OkHttpRequest 对象.
     *
     * @return
     */
    public BaseHttpRequest getOkHttpRequest() {
        return okHttpRequest;
    }

    /**
     * 生成 OkHttp Call 对象
     *
     * @return
     */
    public Call buildCall() {
        request = generateRequest(); //获取 request 请求
        if (readTimeOut > 0 || writeTimeOut > 0 || connTimeOut > 0) {
            //设置超时时间
            readTimeOut = readTimeOut > 0 ? readTimeOut : DEFAULT_TIME;
            writeTimeOut = writeTimeOut > 0 ? writeTimeOut : DEFAULT_TIME;
            connTimeOut = connTimeOut > 0 ? connTimeOut : DEFAULT_TIME;

            client = HttpUtils.getInstance().getOkHttpClient().newBuilder()
                    .readTimeout(readTimeOut, TimeUnit.MILLISECONDS)
                    .writeTimeout(writeTimeOut, TimeUnit.MILLISECONDS)
                    .connectTimeout(connTimeOut, TimeUnit.MILLISECONDS)
                    .build();

            call = client.newCall(request);
        } else {
            call = HttpUtils.getInstance().getOkHttpClient().newCall(request);
        }
        return call;
        /**
         * ps :
         *  TimeUnit.DAYS         日的工具类
         *  TimeUnit.HOURS        时的工具类
         *  TimeUnit.MINUTES      分的工具类
         *  TimeUnit.SECONDS      秒的工具类
         *  TimeUnit.MILLISECONDS 毫秒的工具类
         */

    }

    /**
     * 执行异步请求
     *
     * @param callback
     */
    public void enqueue(BaseHttpCallBack callback) {
        buildCall();  //设置request
        //执行请求.设置calback
        HttpUtils.getInstance().enqueue(this, callback);
    }

    /**
     * 执行同步请求
     *
     * @return 请求返回的结果集
     * @throws IOException 请求异常
     */
    public Response execute() throws IOException {
        buildCall();
        return HttpUtils.getInstance().execute(this);
    }

    /**
     * 同步方法执行回调请求
     *
     * @param callBack
     */
    public void executeCallBack(BaseHttpCallBack callBack) {
        buildCall();
        HttpUtils.getInstance().executeCallBack(this, callBack);
    }

    /**
     * 创建一个 socket 链接
     *
     * @param webSocketListener
     */
    public void newWebSocket(WebSocketListener webSocketListener){
        buildCall();
        HttpUtils.getInstance().newWebSocket(request, webSocketListener);
    }

    /**
     * 获取 call 对象.
     *
     * @return
     */
    public Call getCall() {
        return this.call;
    }

    /**
     * 生成 OkHttp请求
     *
     * @return
     */
    private Request generateRequest() {
        return okHttpRequest.generateRequest();
    }

}
